package com.harry.configuration.web.interceptor.outest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.harry.configuration.ConstSettings;
import com.harry.configuration.data_interchange.RpcResult;
import com.harry.utility.InnerUtils;

@RestController
@ControllerAdvice
public class WebApiExceptionHandler {

	// Logic error
	@ExceptionHandler(value = Throwable.class)
	@ResponseBody
	public Object defaultErrorHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) throws Exception {
		InnerUtils.makeCorsHeader(resp);
		return new RpcResult(e);
	}

	// 404 Not found error
	@RequestMapping("/**/*")
	@ResponseBody
	public Object error(HttpServletRequest req, HttpServletResponse resp, Exception e) throws Exception {
		InnerUtils.makeCorsHeader(resp);
		return new RpcResult(ConstSettings.notFound);
	}
}